//
//  KSFormHItem.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/8/23.
//  Copyright © 2021 aowang. All rights reserved.
//
import WebKit
import UIKit
enum KType {
   
   
 
    //   segment
    //   纯提示的红色文字
    //  带加减号的数字输入
    //  webview
    // 图片
    // collection
    // 定制的一个个人信息类型 uinfo,addrInfo,yewuyuan,sign
    case switchx, // 开关
         tempTextView, //   文本 支持多行 动态变高
         singleSelect,   //  单选
         segment,tempTip,map, countV,web,image,uinfo,addrInfo,yewuyuan,sign,progress,seperate
}



var widgetLeading:CGFloat = 6

class KSFormBaseItem: FormItem {
    
     var type:KType! = .tempTextView
    var appearance: KSFormBaseAppear!
//记录上一次值
    var previousTextViewStr: String = ""

    lazy var segment:UISegmentedControl = {
       let v = UISegmentedControl()
       return v
   }()
     lazy var tpTitleLab:UILabel = {
        let v = UILabel()
        return v
    }()
    
    lazy var textView:UITextView_P = {
        let v = UITextView_P()
         v.font = UIFont.systemFont(ofSize: 16)
        return v
    }()
    lazy var asterisLab:UILabel = {
       let v = UILabel()
        v.text = "*"
        v.textColor = .red
        v.isHidden = true
        v.font = UIFont.systemFont(ofSize: 20)
       return v
   }()
   
    // 尾随的 文本  (单位)等
    lazy var descLab:UILabel = {
        let v = UILabel()
        v.font = UIFont.systemFont(ofSize: 14)
        return v
    }()
    // 纯红色的提示性文字
     lazy var tipLab:UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
        v.font = UIFont.systemFont(ofSize: 17)
        v.textColor = .red
        return v
    }()
    
    lazy var countV:KSCountView = {
        let v = KSCountView()
         return v
    }()
    
   
  
    lazy var imageV:UIImageView = {
        let v = UIImageView()
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5
         return v
    }()
   
    // 右箭头
//    lazy var arrowMark:UIImageView = {
//        let v = UIImageView()
//        v.image = UIImage(named:"formitem_right")
//        return v
//    }()
    // 右边辅助图标
    lazy var assistBtn:UIButton = {
        let v = UIButton()
         return v
    }()
    var disabled:Bool = false {
        didSet {
            self.disabledChange()
        }
    }
    func disabledChange() {
        // 可编辑状态改变  子类实现
    }
    func actionClosureChange() {
        //点击事件改变 子类覆盖实现
    }
  
    func layoutArrow() {
       // 子类实现
    }
   
    var selectBtnArr = [UIButton]()
    
    
    // MARK: - 设置 事件回调 set action
    var actionClosure:(()->())? {
        didSet {
           actionClosureChange()
        }
    }
  
    
    //跟随图标适应  【searchFlag图标和内容控件联合布局】
    func assistImgAdapt() {
        if self.appearance.assistImgStr.isSome {
            assistBtn.setBackgroundImage(UIImage(named:self.appearance.assistImgStr.unwrap), for: .normal)
        } else {
            if actionClosure.isSome {
                if self.appearance.autoArrow {
               
                    assistBtn.setBackgroundImage(UIImage(named:"formitem_right"), for: .normal)
                  } else {
                      assistBtn.setBackgroundImage(nil, for: .normal)
                  }
             } else {
                if disabled {
                    assistBtn.setBackgroundImage(nil, for: .normal)
                 } else {
                    assistBtn.setBackgroundImage(UIImage(named:"tenderBJ"), for: .normal)
 
                  }
            }
        }
      
    }
    
    //标题颜色的适应
    func titleLabColorAdapt() {
        // 标题根据是否可编辑变色
         if disabled {
            self.tpTitleLab.textColor = UIColor(kshex:"888888")
         } else {
            self.tpTitleLab.textColor = self.appearance.tpTitleColor
         }
        
        // 标题默认全灰
//        self.tpTitleLab.textColor = titleColorLightGray
    }
    
    func textViewColorAdapt() {
        // 内容处理颜色 (原始方式，备用，以防新方式用户不喜欢)
        if disabled {
             if self.appearance.disableStyleContentGray {
                self.textView.textColor = titleColorLightGray
            }
         } else {

             self.textView.textColor = self.appearance.tpTitleColor
         }
//        self.textView.backgroundColor = disabled ? self.appearance.itembgColor : self.appearance.textBgColor
        self.textView.backgroundColor =  self.appearance.itembgColor
    }
    
    var singleSeclectChangeClo:((Int)->())?
    var textValueChangeClo:((String)->())?
    var textEndEditClo:((String)->())?
    var assistBtnAction:(()->())?
    var countViewChangeClo:((String)->())?

    var segmentChangeClo:((Int,String)->())?
    
    
    var lastBtn:UIButton? = nil
 
    func  addTextField() {
       // 子类覆盖实现
    }
    func  tempApddTextView() {
        // 子类覆盖实现
   }
    // 【searchFlag图标和内容控件联合布局】
    func  layoutTextView() {
        // 子类覆盖实现
   }
    func layoutTitle() {
        // 子类覆盖实现
    }
        // tag 便于获取该控件
    convenience init(title:String,appearance: KSFormBaseAppear? = KSFormBaseAppear(),type:KType = .tempTextView) {
        let hhh = getHeightFromItem(appearance: appearance, type: type)
        let rec = CGRect(origin: .zero, size: CGSize(width: KSGlobal.screenW - BgLeading * 2, height: hhh))
        self.init(frame:rec)
        self.appearance = appearance
        self.backgroundColor = self.appearance.itembgColor
        
        if self.appearance.titleFont.isSome {
            self.tpTitleLab.font = self.appearance.titleFont!
        }
        
        self.setRoundType(type: self.appearance.roundType)
        self.configTitle(title)
        
       
       
        self.type = type
        if type == .tempTextView {
            self.tempApddTextView()
        } else if type == .singleSelect && self.appearance.selectConArr.isSome {
             self.addSigleSeArr()
        } else if type == .segment && self.appearance.segTitleArr.isSome {
            self.addSegment()
        } else if type == .tempTip {
            self.addTipLab()
        } else if type == .countV {
            self.addCountView()
        } else if type == .image {
            self.addImage()
        }
        if self.appearance.phoneInput {
            self.setPhoneInput()
        }
        if self.appearance.twoDemiInput {
            self.setDemiKeyborad()
        }
        if self.appearance.negativeTwoDemiInput {
            self.setDemiKeyborad()
        }
        if self.appearance.threeDemiInput {
            self.setDemiKeyborad()
        }
        if self.appearance.fourDemiInput {
            self.setDemiKeyborad()
        }
        if self.appearance.integerInput {
            self.setIntegerInput()
        }
        if self.appearance.secureEntry {
            self.setSecureEntry(true)
        }
      
     
        if self.appearance.tailDesc.isSome {
           self.addDesc()
           self.descLab.text = self.appearance.tailDesc!
       }
     
        self.addAssistImg()
         
     
    }
    // 过程中更新尾部提示词（单位，等）
    func updateTailDesc() {
        if self.appearance.tailDesc.isSome {
           self.addDesc()
           self.descLab.text = self.appearance.tailDesc!
            self.layoutTextView()
       }
    }
    func addAssistImg() {
        // 子类实现
    }
    // 必填星号
   private func addAsteris() {
        self.addSubview(asterisLab)
        asterisLab.snp.makeConstraints { make in
            make.left.equalTo(tpTitleLab.snp.right).offset(2)
            make.top.equalTo(self.tpTitleLab)
         }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.addSubview(tpTitleLab)
         self.layoutTitle()
        self.addAsteris()
 
    
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 获取内容 用于判空等
    func contValue() -> String {
        var str:String?
        if self.type == .segment {
            str = "\(self.segment.selectedSegmentIndex)"
        } else if self.type == .countV {
            str = "\(self.countV.currentNumber)"
        } else {
            if self.textView.text.unwrap != "" {
                str = self.textView.text
            }
        }
        
        return str.unwrap
    }
}



class ItemSectionView:FormItem {
    
    var deleteClo:(()->())?
    lazy var lab:UILabel = {
       let v = UILabel()
        v.font = UIFont.systemFont(ofSize: 16)
        v.textColor = .darkGray
        return v
    }()
    lazy var btn:UIButton = {
       let v = UIButton()
        v.setTitle("删除", for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        v.setTitleColor(Theme.ORANGE, for: .normal)
        return v
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setLineHidden(true)
        self.backgroundColor = Theme.commonBg
        self.addSubview(lab)
        self.addSubview(btn)
         lab.snp.makeConstraints { make in
           make.left.equalTo(self).offset(10)
           make.centerY.equalTo(self)
       }
        btn.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-10)
            make.centerY.equalTo(self)
            make.width.equalTo(50)
            make.height.equalTo(30)
        }
        btn.addTouchUpInSideAction{ _ in
            if let clo = self.deleteClo {
                clo()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class FormItButton:UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        self.setTitleColor(.darkGray, for: .normal)
        self.setImage(UIImage(named: "ksitem_unselect"), for: .normal)
        self.setImage(UIImage(named: "ksitem_select"), for: .selected)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 设置title
     imageView?.frame = CGRect(x: 0 , y: 4, width: 22, height: 22)
        // 设置imageView
        titleLabel?.frame = CGRect(x: 30 , y: 0, width: self.frame.width - 22, height: self.frame.height)
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
